package com.sgb.kjwl.view.ui.activity.material;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.SparseBooleanArray;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityStListBinding;
import com.sgb.kjwl.di.material.DaggerMaterialComponent;
import com.sgb.kjwl.di.material.MaterialModule;
import com.sgb.kjwl.model.entity.MaterialFilterEntity;
import com.sgb.kjwl.model.entity.STFilterEntity;
import com.sgb.kjwl.model.entity.request.MaterialFilterJson;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.user.SearchSTEntity;
import com.sgb.kjwl.view.ui.activity.MainActivity;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;
import com.sgb.kjwl.view.ui.adapter.search.SearchSTAdapter;
import com.sgb.kjwl.view.widget.CommercialConcreteFliterPopWindow;
import com.sgb.kjwl.view.widget.DictWindow4;
import com.sgb.kjwl.view.widget.DictWindow5;
import com.sgb.kjwl.view.widget.MultiWindow4;
import com.sgb.kjwl.viewmodel.material.CommercialConcreteListModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 * 商砼列表
 */
public class CommercialConcreteListActivity extends AppActivity implements BasePopWindow.OnResultClick {

    @Inject
    CommercialConcreteListModel viewModel;
    private ActivityStListBinding binding;
    private String title;
    private String id;
    private String mId;  //用户筛选类别查询
    private int timeSort = 1;
    private int priceSort = 1;
    private int tempPriceSort = 1;
    private int tempTimeSort = 1;
    private SearchSTAdapter adapter;
    private STFilterEntity stFilterEntity;
    private CommercialConcreteFliterPopWindow popWindow;
    private DictWindow5 levelWindow5;
    private String levelChecked = "";
    private List<DictEntity3> levelDictEntityList;
    private MultiWindow4 storeMultiWindow4;
    private List<DictEntity3> storeDictEntityList;
    private DictWindow5 deviceWindow5;
    private String deviceChecked = "";
    private List<DictEntity3> deviceDictEntityList;
    private DictWindow5 serviceWindow5;
    private String serviceChecked = "";
    private List<DictEntity3> serviceDictEntityList;
    private String distanceChecked = "";
    private DictWindow4 distanceDistanceWindow4;
    private List<DictEntity3> distanceDictEntityList;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMaterialComponent.builder().appComponent(appComponent).materialModule(new MaterialModule(this)).build().inject(this);
    }

    public static void start(Context context, String title, String id, String mId) {
        Intent intent = new Intent();
        intent.putExtra("title", title);
        intent.putExtra("id", id);
        intent.putExtra("mId", mId);
        intent.setClass(context, CommercialConcreteListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_st_list);
        binding.ivBack.setVisibility(View.VISIBLE);
        binding.ivBack.setOnClickListener(this);
        binding.ivCart.setOnClickListener(this);
        binding.priceLayout.setOnClickListener(this);
        binding.timeLayout.setOnClickListener(this);
        binding.filterLayout.setOnClickListener(this);
        binding.levelLayout.setOnClickListener(this);
        binding.distanceLayout.setOnClickListener(this);
        binding.storeLayout.setOnClickListener(this);
        binding.deviceLayout.setOnClickListener(this);
        binding.serviceLayout.setOnClickListener(this);

        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_line)));

        adapter = new SearchSTAdapter(getActivity(), null);
        adapter.setOnItemClick(new AdapterOnItemClick<SearchSTEntity>() {
            @Override
            public void onItemClick(SearchSTEntity searchSTEntity, int position) {
                MaterialFilterJson filterJson = new MaterialFilterJson();
                if (TextUtils.isEmpty(viewModel.materialFilterJson.get().getStrengthId())) {
                    filterJson.setStrengthId(searchSTEntity.getStrengthId());
                } else {
                    filterJson.setStrengthId(viewModel.materialFilterJson.get().getStrengthId());
                }
                filterJson.setGradationId(viewModel.materialFilterJson.get().getGradationId());
                filterJson.setServiceId(viewModel.materialFilterJson.get().getServiceId());
                ConcreteDetailActivity2.start(getActivity(), searchSTEntity.getId(), filterJson);
            }
        });
        binding.recyclerView.setAdapter(adapter);

        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getSTModel(false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getSTModel(true);
            }
        });

        popWindow = new CommercialConcreteFliterPopWindow(getActivity());
        popWindow.setBgView(binding.grayLayout);
        popWindow.setFilterResultClick(new CommercialConcreteFliterPopWindow.FilterResultClick() {
            @Override
            public void result(MaterialFilterJson materialFilterJson) {
                viewModel.materialFilterJson.get().setAreaId(materialFilterJson.getAreaId());
                viewModel.materialFilterJson.get().setStrengthId(materialFilterJson.getStrengthId());
                viewModel.materialFilterJson.get().setGradationId(materialFilterJson.getGradationId());
                viewModel.materialFilterJson.get().setServiceId(materialFilterJson.getServiceId());
                viewModel.getSTModel(false);
            }

            @Override
            public void queryTypeChild(String id) {
            }

            @Override
            public void queryNameChild(String id) {
            }
        });
        initFilterView();
    }

    private void initFilterView() {
        levelWindow5 = new DictWindow5(CommercialConcreteListActivity.this);
        levelWindow5.setOnCallBack(new DictWindow5.OnCallBack() {
            @Override
            public void result(DictEntity3 dictEntity) {
                levelChecked = dictEntity.getName();
                viewModel.materialFilterJson.get().setStrengthId(dictEntity.getId());
                viewModel.getSTModel(false);
            }
        });
        levelWindow5.setOnCloseBack(new DictWindow5.OnCloseBack() {
            @Override
            public void dismiss() {
                binding.levelArrowView.setImageResource(R.mipmap.ic_down_arrow_n);
            }
        });
        storeMultiWindow4 = new MultiWindow4(CommercialConcreteListActivity.this);
        storeMultiWindow4.setOnCallBack(new MultiWindow4.OnCallBack() {
            @Override
            public void result(List<String> storeIds, List<DictEntity3> checkDict3List,  SparseBooleanArray checkBooleanArray) {
                //viewModel.materialFilterJson.get().setAreaId(storeIds);
                viewModel.getSTModel(false);
            }
        });
        storeMultiWindow4.setOnCloseBack(new MultiWindow4.OnCloseBack() {
            @Override
            public void dismiss() {
                binding.storeArrowView.setImageResource(R.mipmap.ic_down_arrow_n);
            }
        });
        deviceWindow5 = new DictWindow5(CommercialConcreteListActivity.this);
        deviceWindow5.setOnCallBack(new DictWindow5.OnCallBack() {
            @Override
            public void result(DictEntity3 dictEntity) {
                deviceChecked = dictEntity.getName();
                viewModel.materialFilterJson.get().setGradationId(dictEntity.getId());
                viewModel.getSTModel(false);
            }
        });
        deviceWindow5.setOnCloseBack(new DictWindow5.OnCloseBack() {
            @Override
            public void dismiss() {
                binding.diviceArrowView.setImageResource(R.mipmap.ic_down_arrow_n);
            }
        });
        serviceWindow5 = new DictWindow5(CommercialConcreteListActivity.this);
        serviceWindow5.setOnCallBack(new DictWindow5.OnCallBack() {
            @Override
            public void result(DictEntity3 dictEntity) {
                serviceChecked = dictEntity.getName();
                viewModel.materialFilterJson.get().setServiceId(dictEntity.getId());
                viewModel.getSTModel(false);
            }
        });
        serviceWindow5.setOnCloseBack(new DictWindow5.OnCloseBack() {
            @Override
            public void dismiss() {
                binding.serviceArrowView.setImageResource(R.mipmap.ic_down_arrow_n);
            }
        });
        distanceDistanceWindow4 = new DictWindow4(CommercialConcreteListActivity.this);
        distanceDistanceWindow4.setOnCallBack(new DictWindow4.OnCallBack() {
            @Override
            public void result(DictEntity3 dictEntity) {
                distanceChecked = dictEntity.getName();
                viewModel.materialFilterJson.get().setDistance(dictEntity.getId());
                viewModel.getSTModel(false);
            }
        });
        distanceDistanceWindow4.setOnCloseBack(new DictWindow4.OnCloseBack() {
            @Override
            public void dismiss() {
                binding.distanceArrowView.setImageResource(R.mipmap.ic_down_arrow_n);
            }
        });
    }

    @Override
    protected void init() {
        title = getIntent().getStringExtra("title");
        id = getIntent().getStringExtra("id");
        mId = getIntent().getStringExtra("mId");
        binding.tvTitle.setText(title);
        viewModel.materialFilterJson.get().setItemCid(mId);
        viewModel.materialFilterJson.get().setItemId(id);
        viewModel.getSTModel(false);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_cart:
                if ("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    VerificationCodeLoginActivity.start(this);
                } else {
                    viewModel.checkToken("add_shopping_car");
                }
                break;

            case R.id.price_layout:
                timeSort = 0;
                binding.timeImg.setImageResource(R.mipmap.serch_default);
                if (priceSort == 0) {
                    priceSort = tempPriceSort;
                }
                if (priceSort == 1) {
                    priceSort = 2;
                    tempPriceSort = 2;
                    binding.priceImg.setImageResource(R.mipmap.serch_down);
                } else {
                    priceSort = 1;
                    tempPriceSort = 1;
                    binding.priceImg.setImageResource(R.mipmap.serch_up);
                }
                viewModel.materialFilterJson.get().setTimeSort(timeSort);
                viewModel.materialFilterJson.get().setPriceSort(priceSort);
                viewModel.getSTModel(false);
                break;
            case R.id.time_layout:
                priceSort = 0;
                binding.priceImg.setImageResource(R.mipmap.serch_default);
                if (timeSort == 0) {
                    timeSort = tempTimeSort;
                }
                if (timeSort == 1) {
                    timeSort = 2;
                    tempTimeSort = 2;
                    binding.timeImg.setImageResource(R.mipmap.serch_up);
                } else {
                    timeSort = 1;
                    tempTimeSort = 1;
                    binding.timeImg.setImageResource(R.mipmap.serch_down);
                }
                viewModel.materialFilterJson.get().setPriceSort(priceSort);
                viewModel.materialFilterJson.get().setTimeSort(timeSort);
                viewModel.getSTModel(false);
                break;

            case R.id.filter_layout:
                popWindow.showPopwindow(binding.filterLayout);
                if (stFilterEntity == null) {
                    viewModel.getSTFilterTypeModel();
                }
                break;
            case R.id.level_layout:
                binding.levelArrowView.setImageResource(R.mipmap.ic_up_arrow_n);
                levelWindow5.setCheckDefaultByName(levelChecked);
                levelWindow5.showPopwindow(binding.levelLayout);
                if (levelDictEntityList == null) {
                    viewModel.getSTFilterTypeModel();
                }
                break;
            case R.id.store_layout:
                binding.storeArrowView.setImageResource(R.mipmap.ic_up_arrow_n);
                storeMultiWindow4.showPopwindow(binding.storeLayout);
                if (storeDictEntityList == null) {
                    viewModel.getSTFilterTypeModel();
                }
                break;
            case R.id.device_layout:
                binding.diviceArrowView.setImageResource(R.mipmap.ic_up_arrow_n);
                deviceWindow5.setCheckDefaultByName(deviceChecked);
                deviceWindow5.showPopwindow(binding.deviceLayout);
                if (deviceDictEntityList == null) {
                    viewModel.getSTFilterTypeModel();
                }
                break;
            case R.id.service_layout:
                binding.serviceArrowView.setImageResource(R.mipmap.ic_up_arrow_n);
                serviceWindow5.setCheckDefaultByName(serviceChecked);
                serviceWindow5.showPopwindow(binding.serviceLayout);
                if (serviceDictEntityList == null) {
                    viewModel.getSTFilterTypeModel();
                }
                break;
            case R.id.distance_layout:
                binding.distanceArrowView.setImageResource(R.mipmap.ic_up_arrow_n);
                distanceDistanceWindow4.setCheckDefault(distanceChecked);
                distanceDistanceWindow4.showPopwindow(binding.distanceLayout);
                if (distanceDictEntityList == null) {
                    viewModel.getFilterDistance();
                }
                break;
        }

    }

    public void showList(List<SearchSTEntity> list, boolean isLoadMore) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() != 0) {
                adapter.addDatas(list);
            }
        } else {
            if (list.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }

            binding.recyclerView.refreshComplete();
            binding.recyclerView.setLoadingMoreEnabled(true);
            adapter.setDatas(list);
        }
    }

    public void showPop(STFilterEntity data) {
        this.stFilterEntity = data;
        if (data == null) {
            return;
        }
        popWindow.setData(data);
        if (data.getStrength() == null || data.getStrength().size() == 0) {
            return;
        }
        this.levelDictEntityList = data.getStrength();
        levelChecked =data.getStrength().get(0).getName();
        levelWindow5.setData(data.getStrength());
        levelWindow5.setCheckDefaultByName(levelChecked);
        if (data.getArea() == null || data.getArea().size() == 0) {
            return;
        }
        this.storeDictEntityList = data.getArea();
        storeMultiWindow4.setData(data.getArea());
        if (data.getGradation() == null || data.getGradation().size() == 0) {
            return;
        }
        this.deviceDictEntityList = data.getGradation();
        deviceChecked = data.getGradation().get(0).getName();
        deviceWindow5.setData(data.getGradation());
        deviceWindow5.setCheckDefaultByName(deviceChecked);
        if (data.getServiceType() == null || data.getServiceType().size() == 0) {
            return;
        }
        this.serviceDictEntityList = data.getServiceType();
        serviceChecked = data.getServiceType().get(0).getName();
        serviceWindow5.setData(data.getServiceType());
        serviceWindow5.setCheckDefaultByName(serviceChecked);
    }

    public void showDistancePopWindow(List<DictEntity3> data) {
        this.distanceDictEntityList = data;
        if (data == null || data.size() == 0) {
            return;
        }
        distanceChecked = data.get(0).getName();
        distanceDistanceWindow4.setData(data);
        distanceDistanceWindow4.setCheckDefault(distanceChecked);
    }

    @Override
    public void result(Object key1, Object key2, Object key3) {

    }

    public void showAreaPop(List<MaterialFilterEntity> data) {

    }

    public void checkTokenView() {
        Intent intent = new Intent();
        intent.putExtra("showPage", "FRAGMENT_SHOPPING");
        MainActivity.start(CommercialConcreteListActivity.this, intent);
    }

    public void toLogin() {
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_TOKEN, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PHONE, "");
        MToast.showToast(this, "登录失效，请重新登录");

        VerificationCodeLoginActivity.start(this);
    }
}
